package com.itfreer.bpm.workflow.utils;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.bpm.config.entity.BpmTaskConfigEntity;
import com.itfreer.bpm.config.service.IBpmTaskConfigService;
import com.itfreer.bpm.flow.findwork.para.FindWorkInfo;
import com.itfreer.bpm.history.dao.IBpmHistoryDao;
import com.itfreer.bpm.history.entity.BpmHistoryEntity;
import com.itfreer.bpm.option.para.UserAuth;
/**
 * 用户授权解析工具
 * @author gj
 *
 */
@Component
public class BpmUserAuthUtils {
	
	@Autowired
	IBpmTaskConfigService bpmTaskService ;
	
	
	@Autowired
	IBpmHistoryDao historydao;
	
	/**
	 * 用户授权解析
	 * @param userAuth
	 * @param taskConfig
	 */
	public void getUserAuth(UserAuth userAuth,BpmTaskConfigEntity taskConfig) {
		if(taskConfig!=null) {
			if(taskConfig.getOpttj()>0)userAuth.setOpt_tj(true);
			if(taskConfig.getOptth()>0)userAuth.setOpt_th(true);
			if(taskConfig.getOptch()>0)userAuth.setOpt_ch(true);
			if(taskConfig.getOptcx()>0)userAuth.setOpt_cx(true);
		//	if(taskConfig.getOptjh()>0)userAuth.setOpt_jh(true);//只能在流程的第一个环节激活
			if(taskConfig.getOptgq()>0)userAuth.setOpt_gq(true);
			if(taskConfig.getOptjg()>0)userAuth.setOpt_jg(true);
			if(taskConfig.getOpttz()>0)userAuth.setOpt_tz(true);
			if(taskConfig.getOpttz()>0)userAuth.setOpt_tz(true);
			if(taskConfig.getOptbc()>0)userAuth.setOpt_bc(true);
		}
	}
	
	/**
	 * 设置节点服务授权机制
	 * @param info
	 * @param bpmKey
	 * @param taskid
	 * @param taskStatus 节点状态（退件、挂起、空值等）
	 */
	public void setWrokTaskAuth(FindWorkInfo info, String bpmKey,String taskid,String taskStatus) {
		HashMap<String,Object> task_where=new HashMap<String,Object>();
		task_where.put("actdefid:=", bpmKey);
		task_where.put("taskdefid:=", taskid);
		
		List<BpmTaskConfigEntity> task_configs=bpmTaskService.getEntitys(null, task_where, null, 1, 1);
		if(task_configs!=null && task_configs.size()>0) {
			BpmTaskConfigEntity taskConfig=task_configs.get(0);
			info.setCtab(taskConfig.getPrjtab());
			info.setPrjshow(taskConfig.getPrjshow());
			/*UserAuth userAuth=new UserAuth();*/
			if("退件".equals(taskStatus)) {
				info.getAuth().setOpt_jh(true);
				info.getAuth().setOpt_cx(true);
			}else {
				this.getUserAuth(info.getAuth(),taskConfig);
			}
		
			/*info.setAuth(info.getAuth());	*/
		}
	}
	
	/**
	 * 设置项目撤回权限
	 */
	public void setWithdraw(FindWorkInfo info, String bpmKey,String taskid) {
		HashMap<String,Object> task_where=new HashMap<String,Object>();
		task_where.put("actdefid:=", bpmKey);
		task_where.put("taskdefid:=", taskid);
		
		List<BpmTaskConfigEntity> task_configs=bpmTaskService.getEntitys(null, task_where, null, 1, 1);
		if(task_configs!=null && task_configs.size()>0) {
			BpmTaskConfigEntity taskConfig=task_configs.get(0);
			if(taskConfig.getOptch()>0) {
				info.getAuth().setOpt_ch(true);
			}
		}
	}
	/**
	 * 查找是否存在未来需要办理的待办项目
	 * @param sexeid
	 * @return
	 */
	public boolean getFutureWork(String sexeid) {
		HashMap<String,Object> history_where=new HashMap<String,Object>();
		history_where.put("sexeid:=", sexeid);
		history_where.put("historystatus:=", "2");
		List<BpmHistoryEntity>lst= historydao.getEntitys(null, history_where, null, 2, 1);
		if(lst!=null && lst.size()>0) return true;
		return false;
	}
}
